#include <pic.h>
#include <stdio.h>
#include <math.h>
#include "lcd3pin.h"
#include "DCI_Protocol.h"
#include "routines.h"
#include "Serial.h"
#include "interrupt.h"
#include "ADC.h"
#include "IO.h"
#include "termistor.h"
#include "version.h"


#define ___DEBUG 4
#define ___QUARTZ 4




//-------------------------------------------------------

extern const char* const Error_explanation[];

extern const char ITUHeader[4];
extern const char ITUPreset[6];
extern bank2 unsigned char ITUMsgId;

extern const char IDUHeader[5];
extern const char IDUPreset[20];
extern bank2 unsigned char IDUMsgId;

//================  T I C K  =============================
// 	
//	 Tick value preloaded into tmr1 counter due to produce a 5ms tick

#if ___QUARTZ == 4
	#define Tick 5000 
#endif

#if ___QUARTZ == 8
	#define Tick 10000 
#endif

//#if ___QUARTZ == 10
//	#define Tick 12500 
//#endif

//#if ___QUARTZ == 20
//	#define Tick 25000 
//#endif


//--------------------------------------------------------

extern bit  flag_system_tick;
extern bit  flag_pulse_tick;
extern bit	flag_packet_received;	 	
extern bit	flag_packet_good;
extern bit	flag_packet_sent;
extern bit	flag_com_pause;
extern bit	flag_rx_error;

extern bit	flag_tout_enable;
extern bit  flag_packet_ready;

//extern bit flag_show_debug;

extern bit flag_test_running;

extern bank2 unsigned char counter1, button;


//--------------------------------------------------------
#define RX_BUF_LENGHT 30
extern bank1 unsigned char rx_buf[RX_BUF_LENGHT];
extern bank1  unsigned char rx_buf_index;
extern bank1  unsigned char rx_buf_start;
extern bank1  unsigned char rx_packet_lengt;
extern bank1  unsigned char rx_byte_tout;
#define TX_BUF_LENGHT 28
extern bank2 unsigned char tx_buf[TX_BUF_LENGHT];
extern bank2 unsigned char tx_buf_index;
extern bank2 unsigned char tx_count;

extern bank1 unsigned char txrxpause;

typedef struct PresetType
{	unsigned char 	Compressor_freq;
	unsigned int 	EEV_value;
	unsigned char	Fan_speed;
	unsigned char	Mode;
};

typedef union FLAGS
{ 	struct{
		unsigned 	Main:1;
		unsigned 	DoTest:1;
		unsigned 	ADC:1;
		unsigned 	ITU:1;
		unsigned 	Interrupt:1;
		unsigned 	Termistor:1;
		unsigned 	:2;
		} Task;
	char Set;
};









extern volatile bank1 en_group sourse,destination;


extern bank1 struct PresetType myPreset;
extern bank1 struct PresetType myAnswer;
extern bank1 unsigned char preset_index;


extern bank1 union ComHeaderType MyODUIDUHeader;
extern bank1 union ComHeaderType MyODUkCOMHeader;
extern bank1 union ComHeaderType MyIDUHeader;
extern bank2 union ODUType MyODU;
extern bank2 union IDUType MyIDU;
extern bank2 union ITUType MyITU;
extern bank2 union ITUType MyITU_answer;


extern union FLAGS flag_packet_tov;

//----------------------------------------------------------

#include "DoTest.h"

extern bank2 int test_duration,rest_of_test,rest_of_stage;

